package com.ajaxit.prisa.pdf;

import java.awt.Color;
import java.awt.Toolkit;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ajaxit.prisa.values.InterventorVO;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;

/**
 *
 * @author Lhy
 */
public class GenerarCertificadoInterventor extends HttpServlet {
    private String nombre;
    private Document documento;
    private String documentoIdentidad;
    private String matricula;
    private String tituloDocumento;

    public GenerarCertificadoInterventor ( ) {
    	super ( );
    }

    public GenerarCertificadoInterventor(String elNombre, String elDocumento, String laMatricula, String elTitulo) {
    	super ( );
        documentoIdentidad = elDocumento;
        nombre = elNombre;
        matricula = laMatricula;
        tituloDocumento = elTitulo;
        
        try {
			doPdf ( new FileOutputStream("C:/Users/Lhy/Documents/Ajax IT Ltda/Sociedad Colombiana de Arquitectectos/Diploma_" + this.nombre + ".pdf") );
		} catch ( FileNotFoundException e ) {
			e.printStackTrace ( );
		}
    }

    public void doGet ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
    	doPdfWeb ( req, resp );    
    }

    private void doPdf ( OutputStream os ) {
        documento = new Document ( new Rectangle ( 932, 710 ) );
        try {
            java.awt.Image awtImage = Toolkit.getDefaultToolkit ( ).createImage ( this.getClass ( ).getResource ( "Diploma.png" ) );
            PdfWriter writer = PdfWriter.getInstance ( documento, os );
            //String password = "prisa";
            //writer.setEncryption ( null, password.getBytes ( ), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128 );
            documento.open ( );
            Image img = Image.getInstance ( awtImage, new Color ( 1 ) );
            img.setAbsolutePosition ( 0, 0 );
            img.setAlignment ( Image.MIDDLE | Image.UNDERLYING );
            documento.add ( img );
            String cadena = "";
            Paragraph p1 = new Paragraph ( "\n", new Font ( Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC ) );
            p1.setAlignment ( Paragraph.ALIGN_CENTER );
            documento.add ( p1 );
            for ( int i = 0; i < 6; i++ ) 
                cadena += "\n";
            cadena += nombre;
            Paragraph p = new Paragraph ( cadena, new Font ( Font.HELVETICA, 30, Font.BOLDITALIC ) );
            p.setAlignment ( Paragraph.ALIGN_CENTER );
            documento.add ( p );
            cadena = "\n";
            for (int i = 0; i < 79; i++) 
                cadena += "\t";
            cadena += documentoIdentidad;
            for (int i = 0; i < 87; i++) 
                cadena += "\t";
            cadena += "   " + matricula;
            Paragraph p2 = new Paragraph ( cadena, new Font ( Font.HELVETICA, 12, Font.NORMAL ) );
            p2.setAlignment ( Paragraph.ALIGN_LEFT );
            documento.add ( p2 );
            documento.addTitle ( tituloDocumento );
        } catch ( Exception e ) {
            System.err.println ( e.getMessage ( ) );
        }
        documento.close ( );
    }

    private void doPdfWeb ( HttpServletRequest req, HttpServletResponse resp ) {
    	resp.setContentType ( "application/pdf" );
    	try {
    		InterventorVO interventor = (InterventorVO)req.getSession ( ).getAttribute ( "interventor" );
       		setMatricula ( interventor.getMatriculaProfesional ( ) );
    		setNombre ( interventor.getNombres ( ) + " " + interventor.getApellidos ( ) );
    		setDocumentoIdentidad ( interventor.getIdentificacionString ( ) );
    		setTituloDocumento ( "Certificado" );
    		doPdf ( resp.getOutputStream ( ) );
		} catch ( IOException e ) {
			e.printStackTrace ( );
		}
    }

    public void doPost ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
    	doPdfWeb(req, resp);
    }

    public void generarCertificado ( ) {
    }

    public Document getDocumento ( ) {
        return documento;
    }

	/**
	 * @return the documentoIdentidad
	 */
	public String getDocumentoIdentidad ( ) {
		return documentoIdentidad;
	}

	/**
	 * @return the matricula
	 */
	public String getMatricula ( ) {
		return matricula;
	}

	/**
	 * @return the nombre
	 */
	public String getNombre ( ) {
		return nombre;
	}

	/**
	 * @return the tituloDocumento
	 */
	public String getTituloDocumento ( ) {
		return tituloDocumento;
	}

	/**
	 * @param documentoIdentidad the documentoIdentidad to set
	 */
	public void setDocumentoIdentidad ( String documentoIdentidad ) {
		this.documentoIdentidad = documentoIdentidad;
	}

	/**
	 * @param matricula the matricula to set
	 */
	public void setMatricula ( String matricula ) {
		this.matricula = matricula;
	}

	/**
	 * @param nombre the nombre to set
	 */
	public void setNombre ( String nombre ) {
		this.nombre = nombre;
	}

	/**
	 * @param tituloDocumento the tituloDocumento to set
	 */
	public void setTituloDocumento ( String tituloDocumento ) {
		this.tituloDocumento = tituloDocumento;
	}

    public static void main ( String args[] ) {
        new GenerarCertificadoInterventor("LINA MARCELA ROJAS", "1234567890", "12345678901234567890", "Certificado");
    }
}
